package main

import (
	"fmt"
	"github.com/Shopify/sarama"
)

func main() {
	// kafka 初始化连接
	config := sarama.NewConfig()
	config.Producer.Return.Successes = true

	client, err := sarama.NewClient([]string{"162.14.70.254:9092"}, config)
	if err != nil {
		panic(err)
	}
	fmt.Println("client start...", client)

	// 创建 topicDetail 设置分区以及副本
	topicDetail := &sarama.TopicDetail{
		NumPartitions:     2,
		ReplicationFactor: 1,
	}
	// 创建 Topic, 首先要创建一个 ClusterAdmin
	admin, err := sarama.NewClusterAdminFromClient(client)
	if err != nil {
		panic(err)
		return
	}
	defer admin.Close()

	err = admin.CreateTopic("topic_new_01", topicDetail, false)
	if err != nil {
		panic(err)
		return
	}
	// 查询所有的topic
	topics, err := admin.ListTopics()
	if err != nil {
		panic(err)
		return
	}
	for topic, detail := range topics {
		fmt.Printf("topic: %s, detail: %+v\n", topic, detail)
	}
}
